﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Business;
using Dto;
using Manage.Filter;

namespace Manage.Controllers
{
    public class HomeController : Controller
    {
        private readonly ManageMenuBusiness _manageMenuBusiness = new ManageMenuBusiness();

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }

        [LoginFilter]
        public ActionResult MainTop()
        {
            var admin = Session["Admin"] as AdminDto;
            if (admin != null)
            {
                List<ManageMenuDto> manageMenuModelList = _manageMenuBusiness.GetMenuBySuperiorId(0, false,
                                                                                                  admin.RoleDto.
                                                                                                      RoleContent.Split(
                                                                                                          new[] { ',' }));
                if (Session["FMenuID"] != null)
                {
                    ViewData["FMenuID"] = Session["FMenuID"].ToString();
                }
                else
                {
                    ViewData["FMenuID"] = manageMenuModelList[0].MenuID;
                }
                return View(manageMenuModelList ?? new List<ManageMenuDto>());
            }
            Response.Write("<script>parent.location.href='/Manage/Login';</script>");
            Response.End();
            return View(new List<ManageMenuDto>());
        }

        [LoginFilter]
        public ActionResult LeftMenu()
        {
            var adminModel = Session["Admin"] as AdminDto;
            List<ManageMenuDto> manageMenuModelList = null;
            var fMenuId = Session["FMenuID"] != null
                              ? Convert.ToInt32(Session["FMenuID"])
                              : _manageMenuBusiness.GetFirstMenu(0);
            if (adminModel != null)
            {
                manageMenuModelList = _manageMenuBusiness.GetMenuBySuperiorId(fMenuId, true,
                                                                              adminModel.RoleDto.RoleContent.Split(
                                                                                  new[] { ',' }));
            }
            return View(manageMenuModelList ?? new List<ManageMenuDto>());
        }

        [LoginFilter]
        public ActionResult Footer()
        {
            return View();
        }

        [LoginFilter]
        public ActionResult ClickFatherMenu(int? id)
        {
            Session["FMenuID"] = id ?? 0;
            Response.Write("<script>parent.location.href='" + Url.Action("Index") + "';</script>");
            Response.End();
            return View("Index");
        }

        public ActionResult ClickChildMenu(int id)
        {
            Session["CMenuID"] = id;
            ManageMenuDto menuDto = _manageMenuBusiness.GetMenuById(id);
            return Redirect(menuDto.MenuPath);
        }

        public ActionResult AdminLogin(string adminName, string adminPwd)
        {
            var business = new AdminBusiness();
            AdminDto model = business.AdminLogin(adminName, adminPwd);
            if (model != null && model.AdminType != 2)
            {
                Session["Admin"] = model;
                return RedirectToAction("Index", "Home");
            }
            TempData["Error"] = true;
            return RedirectToAction("Login", "Home");
        }

        public ActionResult Exit()
        {
            Session.Remove("Admin");
            Response.Write("<script>parent.location.href='" + Url.Action("Login") + "';</script>");
            Response.End();
            return Redirect("/Manage/Login");
        }
    }
}